package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	
	public class board extends Sprite
	{
		static public var stones:Array = [];
		static public var stones_symbs:Array = [];
		static public var size:int;
		static public var move_number:int = 0;
		static public var is_check_ko:Boolean = false;
		static public var is_view_mode:Boolean = false;
		static public var last_eated_count:int = 0; //количество последних съеденных камней
		static public var last_eated_x:int = 0; //координаты последнего съеденного камня
		static public var last_eated_y:int = 0;
		static public var check_ko_deleted_stones:Array = [];
		static public var ko_symb:Sprite = new Sprite();
		static public var lastX:int;
		static public var lastY:int;
		
		public const BACK_COLOR:int = 0xDAB15B //0xeeee77;
		public const FORE_COLOR:int = 0x000000;
		public const CELL_WIDTH:int = 30;
		public const MARGINS:int = 30;
		public const LETTERS:Array = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R","S", "T"];
		public const BLACK_PRISONS:String = "Очки черных";
		public const WHITE_PRISONS:String = "Очки белых";
		
		public var board_x:					int;
		public var board_y:					int;
		public var koX:						int;
		public var koY:						int;		
		public var cursor_stone:			stone;
		public var points_white_label:		TextField 		= new TextField();
		public var caption_white_label:		TextField 		= new TextField();
		public var points_black_label:		TextField 		= new TextField();
		public var caption_black_label:		TextField 		= new TextField();
		public var current_color:			int 			= stone.COLOR_BLACK;
		public var points_white:			int;
		public var points_black:			int;
		public var symb:					Sprite 			= new Sprite();
		public var tree_symb:				tree 			= new tree();
		public var old_stone:				Array 			= [null,null];
		
		public function board(size:int, board_x:int, board_y:int) 
		{
			addEventListener(Event.ADDED_TO_STAGE, init_ui);
			symb.addEventListener(MouseEvent.MOUSE_MOVE, on_mouse_move);
			symb.addEventListener(MouseEvent.MOUSE_OUT, on_mouse_out);
			symb.addEventListener(MouseEvent.CLICK, on_click);
			board.size = size;
			this.x = board_x;
			this.y = board_y;
			clean_stone_arrays();
		}
		
		private function on_mouse_out(e:MouseEvent):void 
		{
			cursor_stone.visible = false;
		}
		
		private function on_click(e:MouseEvent):void 
		{
			if (is_view_mode)
				return;
			var curX:int = get_board_coord_x(e.stageX);
			var curY:int = get_board_coord_y(e.stageY);
			curX = check_correct_coord(curX);
			curY = check_correct_coord(curY);
			if (stones[curX - 1][curY - 1]!=0)
				return;
			if (koX==curX && koY==curY) 
			{
				return;
			}
			else 
			{
				koX = -1;
				koY = -1;
				if (getChildByName("ko")) 
				{
					removeChild(getChildByName("ko"));
				}
				
			}
			lastX = curX;
			lastY = curY;
			move_number++;
			stones[curX - 1][curY - 1] = (current_color == stone.COLOR_BLACK? 1:2);
			check_eat_group(curX, curY);
			var this_group:group = new group(curX, curY);
			if (this_group.dame.length == 0)
			{
				stones[curX - 1][curY - 1] = 0;
				stones_symbs[curX - 1][curY - 1] = null;
				return;
			}
			var nextstone:stone = new stone(current_color, current_coord_x(e.stageX), current_coord_y(e.stageY), CELL_WIDTH / 2);
			addChild(nextstone);
			var tc:treechild = new treechild(current_color, move_number, [curX - 1, curY - 1]);
			tree_symb.addTreeChild(tc);
			var glowf:GlowFilter = new GlowFilter(0xffff00, 1, 10, 10, 3);
			nextstone.filters = [glowf];
			//trace(old_stone[0], old_stone[1]);
			if (old_stone[0] != null)
			{
				if (stones_symbs[old_stone[0]][old_stone[1]])
				stones_symbs[old_stone[0]][old_stone[1]].filters = [];
			}
			old_stone = [curX - 1, curY - 1];
			stones_symbs[curX - 1][curY - 1] = nextstone;
			/*saveKoStates();*/
			if (current_color == stone.COLOR_BLACK)
				current_color = stone.COLOR_WHITE
			else
				current_color = stone.COLOR_BLACK;
			var oldchild:stone = cursor_stone;
			cursor_stone = new stone(current_color, cursor_stone.x, cursor_stone.y, CELL_WIDTH / 2);
			removeChild(oldchild);
			addChild(cursor_stone);
			cursor_stone.alpha = 0.5;
			cursor_stone.visible = false;
		}
		
		private function on_mouse_move(e:MouseEvent):void 
		{
			cursor_stone.visible = true;
			cursor_stone.x = current_coord_x(e.stageX);
			cursor_stone.y = current_coord_y(e.stageY);;
			cursor_stone.mouseEnabled = false;
			cursor_stone.mouseChildren = false;
			var curX:int = get_board_coord_x(e.stageX);
			var curY:int = get_board_coord_y(e.stageY);
			curX = check_correct_coord(curX);
			curY = check_correct_coord(curY);
			cursor_stone.visible = stones[curX - 1][curY - 1] == 0;
			if (stones[curX-1][curY-1]==0) 
			{
				stones[curX - 1][curY - 1] = (current_color == stone.COLOR_BLACK? 1:2);
				var this_group:group = new group(curX, curY);
				stones[curX - 1][curY - 1] = 0;
				if (this_group.dame.length == 0)
				{
					cursor_stone.visible = false;
					return;
				}
			}
		}
		
		private function init_ui(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init_ui);
			var board_width:int = CELL_WIDTH * (size-1);
			var dx:int =  - board_width / 2;
			var dy:int =  - board_width / 2;
			symb.graphics.beginFill(BACK_COLOR);
			symb.graphics.drawRect(dx-MARGINS, dy-MARGINS, board_width + MARGINS*2, board_width + MARGINS*2);
			symb.graphics.endFill();
			graphics.lineStyle(1, 0);
			var s:Sprite = new Sprite();
			s.graphics.lineStyle(1, FORE_COLOR);
			s.graphics.drawRect(0, 0, board_width, board_width);
			s.graphics.lineStyle(1, FORE_COLOR);
			var glowf:GlowFilter = new GlowFilter(0, 1, 5, 5, 1);
			for (var i:int = 1; i < size-1; i++) 
			{
				s.graphics.lineStyle(1, FORE_COLOR);
				s.graphics.moveTo(0, i*CELL_WIDTH);
				s.graphics.lineTo(board_width, i * CELL_WIDTH);
				s.graphics.lineStyle(1, FORE_COLOR);
				s.graphics.moveTo(i*CELL_WIDTH, 0);
				s.graphics.lineTo(i * CELL_WIDTH, board_width);
			}
			var glowf_lines:GlowFilter = new GlowFilter(0, 0.3, 3, 3, 1);
			s.filters = [glowf_lines];
			addChild(symb);
			addChild(s);
			s.mouseEnabled = false;
			s.x = dx;
			s.y = dy;
			
			for (var j:int = 0; j < 19; j++) 
			{
				var label_txt:TextField = new TextField();
				label_txt.text = (j + 1).toString();
				label_txt.y = get_x_coord(j + 1) - label_txt.textHeight / 2;
				label_txt.x = get_x_coord(1) - label_txt.textWidth - 20;
				label_txt.mouseEnabled = false;
				addChild(label_txt);
				label_txt = new TextField();
				label_txt.text = (j + 1).toString();
				label_txt.y = get_x_coord(j + 1) - label_txt.textHeight / 2;
				label_txt.x = get_x_coord(19) - label_txt.textWidth + 28;
				label_txt.mouseEnabled = false;
				addChild(label_txt);
				label_txt = new TextField();
				label_txt.text = LETTERS[j];
				label_txt.x = get_x_coord(j + 1) - label_txt.textWidth / 2;
				label_txt.y = get_x_coord(19) - label_txt.textHeight + 30;
				label_txt.mouseEnabled = false;
				addChild(label_txt);
				label_txt = new TextField();
				label_txt.text = LETTERS[j];
				label_txt.x = get_x_coord(j + 1) - label_txt.textWidth / 2;
				label_txt.y = get_x_coord(1) - label_txt.textHeight - 18;
				label_txt.mouseEnabled = false;
				addChild(label_txt);
			}
			for (var k:int = 0; k < 3; k++) 
			{
				for (var l:int = 0; l < 3; l++) 
				{
					var xc:int = (k * 6) + 4;
					var yc:int = (l * 6) + 4;
					symb.graphics.beginFill(FORE_COLOR);
					symb.graphics.drawCircle(get_x_coord(xc), get_y_coord(yc), CELL_WIDTH / 10);
					symb.graphics.endFill();
				}
			}
			symb.filters = [glowf];
			var points_sprite:Sprite = new Sprite();
			points_sprite.addChild(points_black_label);
			points_sprite.addChild(points_white_label);
			points_sprite.addChild(caption_white_label);
			points_sprite.addChild(caption_black_label);
			points_sprite.x = board_width / 2 + 50;
			points_sprite.y = -board_width/2;
			points_black_label.x = 80;
			points_black_label.y = 0;
			points_white_label.x = 0;
			points_white_label.y = 0;
			caption_white_label.x = 0;
			caption_white_label.y = -10;
			caption_black_label.x = 80;
			caption_black_label.y = -10;
			addChild(points_sprite);
			tree_symb.x = points_sprite.x;
			tree_symb.y = board_width/2 - tree.TREE_HEIGHT;
			addChild(tree_symb);
			points_sprite.filters = [];
			refresh_points();
			cursor_stone = new stone(stone.COLOR_BLACK, 0, 0, CELL_WIDTH / 2);
			addChild(cursor_stone);
			cursor_stone.alpha = 0.5;
			cursor_stone.visible = false;
		}
		
		private function refresh_points():void 
		{
			points_black_label.text = points_black.toString();
			points_white_label.text = points_white.toString();
			caption_white_label.text = WHITE_PRISONS;
			caption_black_label.text = BLACK_PRISONS;
		}
		
		private function checkKo(is_ko:Boolean, curX:int, curY:int):Boolean 
		{
			var res:Boolean = is_ko;
			
			/*stones[curX - 1][curY - 1] = (current_color == stone.COLOR_BLACK? 1:2);
			check_eat_group(curX, curY);
			for (var i:int = 0; i < stones.length; i++) 
			{
				for (var j:int = 0; j < stones[i].length; j++) 
				{
					if (stones[i][j] != ko_state_2back[i][j])
					{
						res = false;
						//trace(i,j,"ko=false")
					}
				}
			}*/
			
			
			return res;
		}
		
		private function prepare_check_ko(curX:int, curY:int):Boolean
		{
			is_check_ko = true;
			var is_ko:Boolean = true;
			check_ko_deleted_stones = [];
			is_ko = checkKo(is_ko, curX, curY);
			if (is_ko)
			{
				for (var i:int = 0; i < check_ko_deleted_stones.length; i++) 
				{
					addChild(check_ko_deleted_stones[i]);
					var returnX:int = get_board_coord_x(check_ko_deleted_stones[i].x + x);
					var returnY:int = get_board_coord_y(check_ko_deleted_stones[i].y + y);
					var returnColor:int = check_ko_deleted_stones[i].color;
					stones[returnX - 1][returnY - 1] = returnColor == stone.COLOR_BLACK ? 1:2;
					stones_symbs[returnX - 1][returnY - 1] = check_ko_deleted_stones[i];
				}
				stones[curX - 1][curY - 1] = 0;
				return is_ko;
			}
			is_check_ko = false;
			return is_ko;
		}
		
		public function clean_stone_arrays():void 
		{
			stones = [];
			stones_symbs = [];
			for (var i:int = 0; i < 19; i++) 
			{
				stones.push([]);
				stones_symbs.push([]);
				for (var j:int = 0; j < 19; j++) 
				{
					stones[i].push(0);
					stones_symbs[i].push(null);
				}
			}
		}
		
		public function get_x_coord(x:int):Number
		{
			return CELL_WIDTH * (check_correct_coord(x) - size / 2 - 1)+CELL_WIDTH/2;
		}
		
		public function get_y_coord(y:int):Number
		{
			return CELL_WIDTH * (check_correct_coord(y) - size / 2 - 1)+CELL_WIDTH/2;
		}
		
		public function check_correct_coord(coord:int):int
		{
			if (coord < 1)
				return 1;
			if (coord > 19)
				return 19;
			return coord;
		}
		
		public function current_coord_x(x:int):int
		{
			return get_x_coord(get_board_coord_x(x));
		}
		
		public function current_coord_y(y:int):int
		{
			return get_y_coord(get_board_coord_y(y));
		}
		
		public function get_board_coord_x(x:Number):int
		{
			return int((x + CELL_WIDTH / 2) / CELL_WIDTH);
		}
		
		public function get_board_coord_y(y:Number):int
		{
			return int((y + CELL_WIDTH / 2) / CELL_WIDTH);
		}
		
		public function check_eat_group(x:int, y:int):void
		{
			if (x<1 || y<1 || x>19 || y>19) 
			{
				return;
			}
			var cur_color:int = stones[x - 1][y - 1];
			var points:int = 0;
			var g_left:group = new group(x - 1, y);
			if (g_left.stones.length > 0 && g_left.dame.length == 0 && g_left.color != cur_color)
			{
				points += g_left.eat();
			}
			var g_right:group = new group(x + 1, y);
			if (g_right.stones.length > 0 && g_right.dame.length == 0 && g_right.color != cur_color)
			{
				points += g_right.eat();
			}
			var g_up:group = new group(x, y - 1);
			if (g_up.stones.length > 0 && g_up.dame.length == 0 && g_up.color != cur_color)
			{
				points += g_up.eat();
			}
			var g_down:group = new group(x, y + 1);
			if (g_down.stones.length > 0 && g_down.dame.length == 0 && g_down.color != cur_color)
			{
				points += g_down.eat();	
			}
			//trace("points=", points);
			if (current_color==stone.COLOR_BLACK) 
			{
				points_black += points;
			}
			else
			{
				points_white += points;
			}
			refresh_points();
		}
		
		public function set_ko(xc:int, yc:int):void
		{
			koX = xc;
			koY = yc;
			var new_ko_symb:Sprite = new Sprite();
			new_ko_symb.name = "ko";
			new_ko_symb.graphics.lineStyle(3, 0);
			new_ko_symb.graphics.moveTo( -7, -7);
			new_ko_symb.graphics.lineTo( 7, 7);
			new_ko_symb.graphics.moveTo( 7, -7);
			new_ko_symb.graphics.lineTo( -7, 7);
			var gf:GlowFilter = new GlowFilter(0xffffff,0.5,2,2,3);
			new_ko_symb.filters = [gf];
			var realX:int= get_x_coord(xc);
			var realY:int= get_y_coord(yc);
			new_ko_symb.x = realX;
			new_ko_symb.y = realY;
			addChild(new_ko_symb);
		}
		
	}
}